/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.stripes.hyperstripes.ext;

import com.farent.solutions.plugins.stripes.hyperstripes.utils.ResourceUtil;
import de.schlichtherle.truezip.file.TFile;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
public class HyperStripesResourceServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        ServletOutputStream stream = null;
        try {
            stream = resp.getOutputStream();
            String url = req.getPathInfo();
            String resourcesFolder = "META-INF/resources";

            //try looking in resources folders
            InputStream is = Thread.currentThread().
                    getContextClassLoader().getResourceAsStream( resourcesFolder + url );
            BufferedInputStream buf = null;

            //now try our resource utilities to see what you can find
            if (is == null) {

                if ( ResourceUtil.isArchiveUrl( url ) ) {
                    String jarFileName = ResourceUtil.getArchiveFileNameWithPath( url );
                    String archiveName = ResourceUtil.getArchiveFileName( url );
                    String resourcePathInjar = ResourceUtil.getArchiveFileNameParts( url )[1];
                    String pathforjar = ResourceUtil.
                            findFirstAsPath( resourcesFolder, jarFileName, Pattern.compile( jarFileName ) );
                    if( pathforjar.startsWith("jar:file:/"))
                        pathforjar = pathforjar.substring("jar:file:/".length());
                    pathforjar = pathforjar.replaceAll("!", "");

                    StringBuilder fileLocation = new StringBuilder();
                    fileLocation.append(pathforjar);
                    fileLocation.append("/").append(archiveName);
                    fileLocation.append(resourcePathInjar);

                    fileLocation.trimToSize();
                    TFile tFile = new TFile(fileLocation.toString());
                    if (tFile.exists())
                        tFile.catTo(stream);
                        return;
                    
                } else {
                    is = ResourceUtil.
                            findFirst( resourcesFolder, url, Pattern.compile( url ) );
                }
            }
            if (is != null) {
                buf = new BufferedInputStream(is);
                int readBytes = 0;
                while ((readBytes = buf.read()) != -1) {
                    stream.write(readBytes);
                }
            }
            else {
                resp.sendError(resp.SC_NOT_FOUND, url + " resource not found");
            }
        } catch (IOException ex) {
            Logger.getLogger(HyperStripesResourceServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
